package com.gamepad;

import mobilefeel.sdk.MobileFeelActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.PowerManager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

public class GamepadActivity extends MobileFeelActivity {

	private PowerManager.WakeLock wakeLock;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        setContentView(getIntent().getIntExtra("LAYOUT", R.layout.gamepad2));

		registerView(R.id.button1, "button1");
		registerView(R.id.button2, "button2");
		if (findViewById(R.id.button3) != null) registerView(R.id.button3, "button3");
		if (findViewById(R.id.button4) != null) registerView(R.id.button4, "button4");
		if (findViewById(R.id.button5) != null) registerView(R.id.button5, "button5");
		if (findViewById(R.id.button6) != null) registerView(R.id.button6, "button6");
		if (findViewById(R.id.button7) != null) registerView(R.id.button7, "button7");
		if (findViewById(R.id.button8) != null) registerView(R.id.button8, "button8");
		if (findViewById(R.id.button9) != null) registerView(R.id.button9, "button9");
		if (findViewById(R.id.button10) != null) registerView(R.id.button10, "button10");
		
		sendCustomData("modo", "gamepad");
        
		PowerManager powerManager= (PowerManager) getSystemService(Context.POWER_SERVICE);
        wakeLock = powerManager.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "Tesis");
        wakeLock.acquire(); 
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.menu, menu);
	    return true;
	}
	

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
	    case R.id.start:
	    	sendCustomData("menu", "start");
	    	return true;
	    case R.id.select:
	    	sendCustomData("menu", "select");
	    	return true;
	    default:
	        return super.onOptionsItemSelected(item);
	    }
	}

	
	
	@Override
    protected void onResume() {
        super.onResume();
        wakeLock.acquire();
	}

	@Override
    protected void onPause() {
        super.onPause();
        wakeLock.release();
    }	

	@Override
	protected void onDestroy() {
		super.onDestroy(false);
		wakeLock.release();
	}
	
	@Override
	public void onBackPressed() {
		sendCustomData("volver a", "modo");
		super.onBackPressed();
	}
	
	@Override
	protected void onDisconnection(boolean remote) {
		Toast.makeText(this, "Desconectado", Toast.LENGTH_SHORT).show();
		Intent intent = new Intent(this, MainActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
		startActivity(intent);
	}
}
